package com.xbongbong.parent.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.helper.ReflectHelper;
import com.xbongbong.saas.enums.MobileDetailSummaryEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.InventoryEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.PaymentTaskEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.business.SupplierContactEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.enums.business.TransferEnum;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;

import java.util.List;

/**
 * @author 吴峰
 * @date 2021/12/17 15:25
 */
public class CommonBusinessFieldUtil {

    /**
     * 获取业务类型主键字段(用于关联列表回显)
     *
     * @param businessType 业务类型
     * @author long.rao
     * @date 2019-02-26 21:50
     */
    public static String getFormEntityMainAttr(Integer businessType) {
        RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(businessType);
        String mainAttr = "";
        switch (redundantTemplateTypeEnum) {
            case CUSTOMER:
                mainAttr = CustomerManagementEnum.NAME.getAttr();
                break;
            case CONTRACT:
                mainAttr = ContractEnum.CONTRACT_NO.getAttr();
                break;
            case CONTACT:
                mainAttr = ContactEnum.NAME.getAttr();
                break;
            case SALES_OPPORTUNITY:
                mainAttr = SalesOpportunityEnum.SERIAL_NO.getAttr();
                break;
            case BOMBILL:
                mainAttr = BomBillEnum.SHEET_NO.getAttr();
                break;
            case COMMUNICATE_PLAN:
                mainAttr = CommunicatePlanEnum.NAME.getAttr();
                break;
            case SUPPLIER:
                mainAttr = SupplierEnum.SUPPLIER_NAME.getAttr();
                break;
            case SUPPLIER_CONTACT:
                mainAttr = SupplierContactEnum.NAME.getAttr();
                break;
            case PURCHASE:
                mainAttr = PurchaseEnum.SHEET_NO.getAttr();
                break;
            case PAYMENT:
                mainAttr = PaymentEnum.PAYMENT_NO.getAttr();
                break;
            case WAREHOUSE:
                mainAttr = WarehouseEnum.WAREHOUSE_NAME.getAttr();
                break;
            case INVENTORY:
                mainAttr = InventoryEnum.SHEET_NO.getAttr();
                break;
            case TRANSFER:
                mainAttr = TransferEnum.SHEET_NO.getAttr();
                break;
            case PRODUCTIONORDER:
                mainAttr = ProductionOrderEnum.SHEET_NO.getAttr();
                break;
            case RETURNED_PURCHASE:
                mainAttr = ReturnedPurchaseEnum.SHEET_NO.getAttr();
                break;
            case PAYMENT_SHEET:
                mainAttr = PaymentSheetEnum.SHEET_NO.getAttr();
                break;
            case PAY_SHEET:
                mainAttr = PaySheetEnum.PAY_PLAN_NO.getAttr();
                break;
            case PAY_PLAN:
                mainAttr = PayPlanEnum.PAY_PLAN_NO.getAttr();
                break;
            case INVOICE:
                mainAttr = InvoiceEnum.SHEET_NO.getAttr();
                break;
            case REFUND:
                mainAttr = RefundEnum.REFUND_NO.getAttr();
                break;
            case WORKORDER:
                mainAttr = WorkOrderEnum.SHEET_NO.getAttr();
                break;
            case QUOTATION:
                mainAttr = QuotationEnum.QUOTATION_NO.getAttr();
                break;
            case PAYMENT_TASK:
                mainAttr = PaymentTaskEnum.PAYMENT_TASK_NO.getAttr();
                break;
            default:
                mainAttr = CustomerManagementEnum.NAME.getAttr();
                break;
        }
        return mainAttr;
    }

    /**
     * 通过业务类型获取简介中显示的字段
     * 重构前 -> com.xbongbong.saas.service.impl.MobileDetailServiceImpl#getShowAttrList(java.lang.Integer)
     * @param businessType
     * @return
     */
    public static JSONArray getShowAttrList(Integer businessType) {
        MobileDetailSummaryEnum summaryEnum = MobileDetailSummaryEnum.getByCode(businessType);
        List<Object> attrList = summaryEnum.getAttrList();
        JSONArray resultArray = new JSONArray();
        for (Object object : attrList) {
            JSONObject itemObj = new JSONObject();
            itemObj.put("attr", ReflectHelper.valueGet(object, "attr"));
            itemObj.put("fieldType", ReflectHelper.valueGet(object, "fieldType"));
            itemObj.put("saasAttr", ReflectHelper.valueGet(object, "saasAttr"));
            resultArray.add(itemObj);
        }
        return resultArray;
    }
}
